<?
class operaciones{

	// Propiedades

	var $id;
	var $descripcion;
	var $pagina;
	var $id_modulo;
	var $id_padre;
	var $tipo;
	var $nivel;
	var $orden;

	var $total;

	function get($conn, $id){
		$q = "SELECT * FROM operaciones ";
		$q.= "WHERE id=$id";
		//die($q);
		$r = $conn->Execute(utf8_encode($q));
		if(!$r->EOF){
			$this->id = $r->fields['id'];
			$this->descripcion = $r->fields['descripcion'];
			$this->pagina = $r->fields['pagina'];
			$this->id_modulo = $r->fields['id_modulo'];
			$this->id_padre = $r->fields['id_padre'];
			$this->tipo = $r->fields['tipo'];
			$this->nivel = $r->fields['nivel'];
			$this->orden = $r->fields['orden'];
			return true;
		}else
			return false;
	}

	function get_all($conn, $id_modulo='0', $orden="orden"){
		$q = "SELECT * FROM operaciones WHERE 1=1 ";
		$q.= (!empty($id_modulo)) ? "AND id_modulo = $id_modulo " : "";
//		$q.= "AND tipo = 'V' ";
		$q.= "ORDER BY $orden ";
		//die($q);
		$r = ($max!=0) ? $conn->SelectLimit(utf8_encode($q), $max, $from) : $conn->Execute(utf8_encode($q));
		$collection=array();
		while(!$r->EOF){
			$ue = new operaciones;
			$ue->get($conn, $r->fields['id']);
			$coleccion[] = $ue;
			$r->movenext();
		}
		$this->total = $r->RecordCount();
		return $coleccion;
	}

	function get_all_by_zona($conn, $id_modulo='0', $orden="orden", $zona){
		$q = "SELECT A.* FROM operaciones AS A ";
		$q.= " INNER JOIN relacion_of_op AS B ON A.id = B.id_operacion ";
		$q.= "WHERE B.id_zona = $zona ";
		$q.= (!empty($id_modulo)) ? "AND A.id_modulo = '$id_modulo' " : "";
//		$q.= "AND tipo = 'V' ";
		$q.= "ORDER BY $orden ";
//		die($q);
		$r = ($max!=0) ? $conn->SelectLimit(utf8_encode($q), $max, $from) : $conn->Execute(utf8_encode($q));
		$collection=array();
		while(!$r->EOF){
			$ue = new operaciones;
			$ue->get($conn, $r->fields['id']);
			$coleccion[] = $ue;
			$r->movenext();
		}
		$this->total = $r->RecordCount();
		return $coleccion;
	}

	function add($conn, $descripcion, $pagina, $id_modulo, $id_padre, $tipo, $nivel1){
		$nivel1 = ($nivel1 == 'on')? "1" : "null";
		$q = "INSERT INTO operaciones ";
		$q.= "(descripcion, pagina, id_modulo, id_padre, tipo, nivel) ";
		$q.= "VALUES ";
		$q.= "('$descripcion', '$pagina', $id_modulo, NULLIF('$id_padre','')::integer, '$tipo', $nivel1 ) ";
		//die($q);
		if($conn->Execute(utf8_encode($q))) {
			$this->auditoria($conn, $q);
			return true;
		} else {
			return false;
		}
	}

	function set($conn, $id, $descripcion, $pagina, $id_modulo, $id_padre, $tipo, $nivel1){
		$nivel1 = ($nivel1 == 'on')? "1" : "null";
		$q = "UPDATE operaciones SET descripcion='$descripcion', pagina = '$pagina', id_modulo = $id_modulo, ";
		$q.= "id_padre = NULLIF('$id_padre','')::integer, tipo = '$tipo', nivel = $nivel1 ";
		$q.= "WHERE id=$id ";
		//die($q);
		if($conn->Execute(utf8_encode($q))) {
			$this->auditoria($conn, $q);
			return true;
		} else {
			return false;
		}
	}

	function del($conn, $id){
		$q = "DELETE FROM operaciones WHERE id=$id";
		if($conn->Execute(utf8_encode($q))) {
			$this->auditoria($conn, $q);
			return true;
		} else {
			return false;
		}
	}

	function has_permiso($conn, $id_usuario, $id_operacion){
		$q = "SELECT id ";
		$q.= "FROM relacion_us_op  ";
		$q.= "WHERE id_usuario= $id_usuario  AND id_operacion = $id_operacion ";
		//die($q);
		//echo $q."<br/>";
		$r = $conn->Execute(utf8_encode($q));
		if(!$r->EOF)
			return true;
		else
			return false;
	}

	function oficina_has_permiso($conn, $id_zona, $id_operacion){
		$q = "SELECT id ";
		$q.= "FROM relacion_of_op  ";
		$q.= "WHERE id_operacion=$id_operacion AND id_zona = $id_zona ";
		//die($q);
		//echo $q."<br/>";
		$r = $conn->Execute(utf8_encode($q));
		if(!$r->EOF)
			return true;
		else
			return false;
	}

	function del_permiso($conn, $id_usuario, $id_operacion){
		$q = "DELETE FROM relacion_us_op WHERE id_usuario=$id_usuario AND id_operacion = $id_operacion ";
		if($conn->Execute(utf8_encode($q))) {
			$this->auditoria($conn, $q);
			return true;
		} else {
			return false;
		}
	}

	function oficina_del_permiso($conn, $id_zona, $id_operacion){
		$q = "DELETE FROM relacion_of_op WHERE id_zona=$id_zona AND id_operacion = $id_operacion ";
		if($conn->Execute(utf8_encode($q))) {
			$this->auditoria($conn, $q);
			return true;
		} else {
			return false;
		}
	}


	function add_permiso($conn, $id_usuario, $id_operacion){
		$this->del_permiso($conn, $id_usuario, $id_operacion);
		$q = "INSERT INTO relacion_us_op ";
		$q.= "(id_usuario, id_operacion) ";
		$q.= "VALUES ";
		$q.= "($id_usuario, $id_operacion ) ";
		if($conn->Execute(utf8_encode($q))) {
			$this->auditoria($conn, $q);
			return true;
		} else {
			return false;
		}
	}

	function oficina_add_permiso($conn, $id_zona, $id_operacion){
		$this->oficina_del_permiso($conn, $id_zona, $id_operacion);
		$q = "INSERT INTO relacion_of_op ";
		$q.= "(id_zona, id_operacion) ";
		$q.= "VALUES ";
		$q.= "($id_zona, $id_operacion ) ";
		if($conn->Execute(utf8_encode($q))) {
			$this->auditoria($conn, $q);
			return true;
		} else {
			return false;
		}
	}
	
	function getAllMods($conn, $orden="id"){
		$q = "SELECT * FROM modulos ";
		$q.= "ORDER BY $orden ";
		$r = $conn->Execute(utf8_encode($q));
		$collection=array();
		while(!$r->EOF){
			$ue = new operaciones;
			$ue->id = $r->fields['id'];
			$ue->descripcion = $r->fields['descripcion'];
			$coleccion[] = $ue;
			$r->movenext();
		}
		return $coleccion;
	}
	
	function getCarpetas($conn, $id_modulo, $orden="id"){
		$q = "SELECT * FROM operaciones WHERE tipo = 'C' AND id_modulo = $id_modulo ";
		//die($q);
		if(!$r = $conn->Execute(utf8_encode($q)))
			return false;
		while(!$r->EOF){
			$id_padre = $r->fields['id_padre'];
			$q = "SELECT descripcion FROM operaciones WHERE id = $id_padre ";
			//die($q);
			$rs = $conn->Execute(utf8_encode($q));
			$collection=array();
			if(!$rs->EOF && !empty($id_padre)){
				$descripcion = $rs->fields['descripcion']. "\\".$r->fields['descripcion'];
			}else{
				$descripcion = $r->fields['descripcion'];
			}
			$ue = new operaciones;
			$ue->id = $r->fields['id'];
			$ue->descripcion = $descripcion;
			$coleccion[] = $ue;
			$r->movenext();
		}
		return $coleccion;
	}

	function auditoria($conn, $query) {
		$id_usuario = $_SESSION['id'];
		$q = "INSERT INTO auditoria(id_usuario, query) VALUES($id_usuario,$$" . $query . "$$);";
		//die($q);
		$conn->Execute(utf8_encode($q));

		return true;
	}		
	
}
?>